package com.sky.mapper;

import com.sky.entity.AddressBook;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface AddressBookMapper {

    @Insert("insert into address_book" +
            "(user_id, consignee, sex, phone, province_code, " +
            "province_name, city_code, city_name, district_code, " +
            "district_name, detail, label, is_default) " +
            "values" +
            "(#{userId},#{consignee},#{sex},#{phone},#{provinceCode}," +
            "#{provinceName},#{cityCode},#{cityName},#{districtCode}," +
            "#{districtName},#{detail},#{label},#{isDefault})")
    void addAddressBook(AddressBook addressBook);

    //获取当前用户登录的所有地址
    @Select("select * from address_book where user_id = #{userId}")
    List<AddressBook> list(Long userId);

    //设置默认地址
    @Update("update address_book set is_default = #{isDefault} where user_id = #{userId}")
    void setDefaultAddressBook(AddressBook addressBook);

    void update(AddressBook addressBook);

    @Select("select * from address_book where user_id = #{userId} and is_default = 1")
    AddressBook getDefaultAddressBook(Long userId);
}
